<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            width: 80%;
            max-width: 400px;
            margin: 50px auto;
            padding: 20px;
            background: #fff;
            border: 1px solid #ddd;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        header {
            background: #333;
            color: #fff;
            padding: 20px;
            text-align: center;
        }

        form {
            margin-top: 20px;
        }

        form h2 {
            color: #333;
            margin-bottom: 20px;
        }

        form label {
            display: block;
            margin-bottom: 5px;
        }

        form input[type="text"],
        form input[type="password"],
        form input[type="email"] {
            width: calc(100% - 20px);
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
        }

        form input[type="button"] {
            width: 100%;
            padding: 10px 20px;
            background: #333;
            color: #fff;
            border: none;
            cursor: pointer;
        }

        form input[type="button"]:hover {
            background: #444;
        }

        .captcha {
            text-align: center;
            margin-bottom: 10px;
        }

        .captcha img {
            border: 1px solid #ddd;
            width: 100px;
            height: 40px;
        }

        .footer {
            background: #333;
            color: #fff;
            text-align: center;
            padding: 10px 0;
            position: relative;
            bottom: 0;
            width: 100%;
        }

        .footer p {
            margin: 0;
        }
    </style>
</head>

<body>
    <header>
        <h1>订阅者注册</h1>
    </header>
    <div class="container">
        <form action="#" method="post">
            <h2>创建您的账户</h2>
            <label for="username">用户名：</label>
            <input type="text" id="username" name="username" required>

            <label for="email">邮箱：</label>
            <input type="email" id="email" name="email" required>

            <label for="password">密码：</label>
            <input type="password" id="password" name="password" required>

            <label for="confirm_password">确认密码：</label>
            <input type="password" id="confirm_password" name="confirm_password" required>

            <div class="captcha">
                <label for="captcha">验证码：</label>
                <img id="captcha" src="http://www.dlrb.com:8082/captcha.php" alt="验证码">
            </div>
            <input type="text" id="captcha_code" name="captcha" placeholder="输入验证码" required>

            <input type="button" class="register" value="注册">

            <p>已经有账户? <a href="login.html">登录</a></p>
        </form>
    </div>
    <footer>
        <p>版权所有 © 2024 用户管理系统</p>
    </footer>
</body>
<script src="./js/jquery-3.7.1.min.js"></script>
<script>
    $(function ($) {
        let captchaimg = document.querySelector("#captcha")
        captchaimg.onclick = function () {
            captcha.src = "http://www.dlrb.com:8082/captcha.php?t=" + Math.random()
        }

        let login = document.querySelector(".register")
        login.onclick = function () {
            let username = document.querySelector("#username")
            let email = document.querySelector("#email")
            let password = document.querySelector("#password")
            let confirm_password = document.querySelector("#confirm_password")
            let captcha = document.querySelector("#captcha_code")
            $.ajax({
                url: "http://www.dlrb.com:8082/register.php",
                type: "POST",
                xhrFields: {
                    withCredentials: true
                },
                data: {
                    username: username.value,
                    mail: email.value,
                    password: password.value,
                    passwordtoo: confirm_password.value,
                    captcha: captcha.value,
                    credentials: 'include'
                },
                dataType: "json",
                success: (resp) => {
                    if (resp.code == 200) {
                        alert(resp.msg)
                        location = "login.html"

                    } else {
                        alert(resp.msg)
                    }
                    captcha.src = "http://www.dlrb.com:8082/captcha.php?t=" + Math.random()
                },
                error: (resp) => {
                    alert(resp.msg)
                    captcha.src = "http://www.dlrb.com:8082/captcha.php?t=" + Math.random()
                }
            })
        }
    })
</script>

</html>